package com.admission.util;

import java.io.IOException;

import org.dom4j.Document;
import org.xml.sax.SAXException;

import com.admission.model.ArtDetail;
import com.admission.model.CollegeDetail;
import com.admission.model.CollegeProvince;
import com.usercalls.BeanToXmlAnaly;
import com.utils.XmlCreateUtil;

public class XmlUtils {

	public static void Create(Object o) {
		BeanToXmlAnaly toXml = new BeanToXmlAnaly();

		toXml.setEncodeing("utf-8", "root");
		Document doc;
		try {
			doc = toXml.beanToXmlDoc(o, true);
			doc.setXMLEncoding("UTF-8");
			if (o instanceof ArtDetail) {
				ArtDetail a = (ArtDetail) o;
				XmlCreateUtil
						.createXml(
								FileUtils.getProjectRoot()
										+ "/WEB-INF/xml/artdetail-"
										+ a.getIdNum() + a.getTicketNum()
										+ ".xml", doc);
			} else if (o instanceof CollegeDetail) {
				CollegeDetail a = (CollegeDetail) o;
				XmlCreateUtil.createXml(
						FileUtils.getProjectRoot()
								+ "/WEB-INF/xml/collegedetail-" + a.getIdNum()
								+ a.getTicketNum() + ".xml", doc);
			} else if (o instanceof CollegeProvince) {
				CollegeProvince a = (CollegeProvince) o;
				XmlCreateUtil.createXml(FileUtils.getProjectRoot()
						+ "/WEB-INF/xml/collegeprovince-" + a.getProvince()
						+ ".xml", doc);
			}

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void create(Object o, String path) {
		try {
			BeanToXmlAnaly toXml = new BeanToXmlAnaly();
			toXml.setEncodeing("utf-8", "root");
			Document doc = toXml.beanToXmlDoc(o, true);
			doc.setXMLEncoding("UTF-8");
			XmlCreateUtil.createXml(path, doc);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
